<?php
/**
 * Action that validates in a user		Registed as 'action/user/valdiate'
 *  
 * $email
 * $pass
 */

#################################################################
#								ACTION
#################################################################
if($_CONTEXT==CONTEXT_ACTION){
		
	$email= urldecode(blg_get_input('e'));
	$email_code= blg_get_input('c');
	
	if(blg_email_exists($email)){
		$user= blg_get_user_entity_by_email($email);

		//USER IS ALREADY VALIDATED!!
		if($user->is_not_active()===FALSE){
			if(blg_user_is_logged_in()===FALSE)
				blg_msg_add(blg_echo('login:do_it'));
			forward();	
		}		
		
		$user_code= blg_user_get_validation_code($user);

		//VALIDATE
		if($user_code == $email_code){
			blg_activate_user($user->guid);
			blg_msg_add(blg_echo('login:success', array($user->name)));
			blg_msg_add(blg_echo('validation:success'));			
			blg_login($user->guid);		
			forward(blg_get_site_url().'user/profile?'.$user->code);
		}	

		//SEND AGAIN EMAIL WITH VALIDATION CODE
		blg_user_send_validation_email($user->guid);
		blg_msg_add(blg_echo('error:security:validation:failed'), true);
		blg_msg_add(blg_echo('register:confirm2', array($user->email)), true);				
		forward();
	}
	
	blg_msg_add(blg_echo('error:security:validation:failed'), true);
	forward();
}
#################################################################
#								ACCESS
#################################################################
elseif($_CONTEXT==CONTEXT_ACCESS){ 
	$_ACCESS= blg_security_grant_from_input(NULL);
}


